﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Time.Utility.Helper
{
    public class DataHelper
    {
        /// <summary>
        /// 将DataTable转换为实体集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> GetList<T>(DataTable dt) where T : new()
        {
            List<T> list = new List<T>();
            var prlist = new List<PropertyInfo>();
            T ob;
            Type t = typeof(T);
            //获得T 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 
            Array.ForEach(t.GetProperties(), p => {
                if (dt.Columns.IndexOf(p.Name) != -1)
                    prlist.Add(p);
            });
            object val;
            string pName;
            foreach (DataRow row in dt.Rows)
            {
                ob = Activator.CreateInstance<T>();
                prlist.ForEach(p => {
                    val = row[p.Name];
                    if (val != DBNull.Value && p.CanWrite)
                    {
                        pName = p.PropertyType.Name;
                        if (pName != val.GetType().Name)
                        {
                            switch (pName)
                            {
                                case "Char":
                                    char c;
                                    Char.TryParse(val.ToString(), out c);
                                    val = c;
                                    break;
                                case "Int32":
                                    int i;
                                    Int32.TryParse(val.ToString(), out i);
                                    val = i;
                                    break;
                                case "Single":
                                    Single s;
                                    Single.TryParse(val.ToString(), out s);
                                    val = s;
                                    break;
                                case "Decimal":
                                    decimal d;
                                    decimal.TryParse(val.ToString(), out d);
                                    val = d;
                                    break;
                                case "DateTime":
                                    DateTime da;
                                    DateTime.TryParse(val.ToString(), out da);
                                    val = da;
                                    break;
                                case "Boolean":
                                    bool b;
                                    bool.TryParse(val.ToString(), out b);
                                    val = b;
                                    break;
                                case "GUID":
                                    Guid g;
                                    Guid.TryParse(val.ToString(), out g);
                                    val = g;
                                    break;
                                case "String":
                                    string ss;
                                    ss = val.ToString();
                                    val = ss;
                                    break;
                            }
                        }
                        p.SetValue(ob, val, null);
                    }
                });
                list.Add(ob);
            }
            return list;
        }
    }
}
